package dji.ux.ext.setting.aircraft;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;

import dji.common.flightcontroller.ConnectionFailSafeBehavior;
import dji.thirdparty.io.reactivex.android.schedulers.AndroidSchedulers;
import dji.ux.beta.R;
import dji.ux.beta.base.ConstraintLayoutWidget;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.SchedulerProvider;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;
import dji.ux.ext.base.view.SelectedOptionPanel;

public class OutOfControlPanel extends ConstraintLayoutWidget {
    private SchedulerProvider schedulerProvider;
    private OutOfControlModel outOfControlModel;
    private Button btn;
    public OutOfControlPanel(@NonNull Context context) {
        super(context);
    }

    public OutOfControlPanel(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public OutOfControlPanel(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        View view = inflate(context, R.layout.ext_panel_out_of_control, this);
        btn = view.findViewById(R.id.button);
        btn.setOnClickListener(view1 -> showPopWindow(btn));
        schedulerProvider = SchedulerProvider.getInstance();
        setClickable(true);
        setFocusable(true);

        if (!isInEditMode()) {
            outOfControlModel = new OutOfControlModel(DJISDKModel.getInstance(),
                    ObservableInMemoryKeyedStore.getInstance(),
                    schedulerProvider);
        }

        setBackgroundColor(Color.BLACK);
        setPadding(25, 25, 25, 25);
    }

    private void showPopWindow(View view) {
        PopupWindow popupWindow = new PopupWindow();
        SelectedOptionPanel panel = new SelectedOptionPanel(getContext());
        panel.setCallback((index, result) -> {
            setMode(index);
            popupWindow.dismiss();
        });

        panel.setCurrentText(((Button) view).getText().toString().trim());

        List<String> data = new ArrayList<>();
        data.add("悬停");
        data.add("降落");
        data.add("返航");
        panel.setData(data);

        popupWindow.setContentView(panel);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setWidth(view.getMeasuredWidth());
        popupWindow.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.showAsDropDown(view, 0, view.getMeasuredHeight());
    }

    @Override
    protected void reactToModelChanges() {
        addReaction(outOfControlModel.getConnectionFailSafeBehavior()
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this::updateText));
    }

    private void updateText(ConnectionFailSafeBehavior connectionFailSafeBehavior){
        switch (connectionFailSafeBehavior.value()) {
            case 0:
                btn.setText("悬停");
                break;
            case 1:
                btn.setText("降落");
                break;
            case 2:
                btn.setText("返航");
                break;
        }
    }

    private void setMode(int index){
        switch (index){
            case 0:
                setFailedBehavior(ConnectionFailSafeBehavior.HOVER);
                btn.setText("悬停");
                break;
            case 1:
                setFailedBehavior(ConnectionFailSafeBehavior.LANDING);
                btn.setText("降落");
                break;
            case 2:
                setFailedBehavior(ConnectionFailSafeBehavior.GO_HOME);
                btn.setText("返航");
                break;
        }
    }

    private void setFailedBehavior(ConnectionFailSafeBehavior connectionFailSafeBehavior){
        addDisposable(outOfControlModel.setConnectionFailSafeBehavior(connectionFailSafeBehavior).subscribe(() -> {
            // Do nothing
        }, logErrorConsumer(TAG, "setAuxiliaryLightModel mode: ")));
    }

    @Override
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        if (!isInEditMode()) {
            outOfControlModel.setup();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        if (!isInEditMode()) {
            outOfControlModel.cleanup();
        }
        super.onDetachedFromWindow();
    }

    @NonNull
    @Override
    public String getIdealDimensionRatioString() {
        return null;
    }
}


